package 综合应用.T4;

import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;
import java.util.Scanner;

public class T4 {

    public static void main(String[] args) throws IOException {

        System.out.println("1.账号登录");
        System.out.println("2.游客浏览");
        Scanner sc = new Scanner(System.in);
        int num = sc.nextInt();

        switch (num){
            case 1:
                login();
                break;
            case 2:
                otherLogin();
                break;
            default:
                System.out.println("请重新输入");
                break;
        }

    }

    public static void otherLogin() throws IOException {
        String date = new SimpleDateFormat("yyyy-MM-dd  HH:mm:ss").format(new Date());
        FileUtils.writeStringToFile(new File("综合应用/T4/data.txt"),"游客 "+date+" 进行浏览！\n",true);
    }

    public static boolean login() throws IOException {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入用户名：");
        String username = sc.nextLine();
        System.out.println("请输入密码：");
        String pwd = sc.nextLine();

        FileInputStream fis = new FileInputStream("综合应用/T4/user.properties");
        Properties pro = new Properties();
        pro.load(fis);
        fis.close();

        String date = new SimpleDateFormat("yyyy-MM-dd  HH:mm:ss").format(new Date());
        for (String s : pro.stringPropertyNames()) {
            if (s.equals(username)&&pro.getProperty(s).equals(pwd)){
                System.out.println("登录成功");
                FileUtils.writeStringToFile(new File("综合应用/T4/data.txt"),s+" "+date+" 登录成功！\n",true);
                return true;
            }
        }
        System.out.println("登录失败");
        FileUtils.writeStringToFile(new File("综合应用/T4/data.txt"),username+" "+date+" 登录失败！\n",true);

        return false;
    }
}
